﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BugNET.DAL;
using BugNET.Common;
using log4net;
using BugNET.Entities;

namespace BugNET.BLL
{
    public class WikiManager
    {
        private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>
        /// Saves the issue
        /// </summary>
        /// <param name="entity">The issue to save.</param>
        /// <returns></returns>
        public static List<Wiki> SaveWikiContent(Wiki entity, ref int resultValue)
        {
            if (entity == null) throw new ArgumentNullException("entity");
            if (entity.ProjectId <= Globals.NEW_ID) throw (new ArgumentException("The wiki project id is invalid"));
            if (string.IsNullOrEmpty(entity.WikiHTML)) throw (new ArgumentException("The wiki content cannot be empty or null"));

            List<Wiki> wikiHistoryList= new List<Wiki>();
            try
            { 
               wikiHistoryList=  DataProviderManager.Provider.SaveWikiContent(entity, ref resultValue);           

            }
            catch (Exception ex)
            {
                Log.Error(LoggingManager.GetErrorMessageResource("SaveWikiError"), ex);                
            }
            return wikiHistoryList;
        }

        /// <summary>
        /// Saves the issue
        /// </summary>
        /// <param name="entity">The issue to save.</param>
        /// <returns></returns>
        public static List<Wiki> SaveWikiPageDetails(Wiki entity, ref int resultValue)
        {
            if (entity == null) throw new ArgumentNullException("entity");
            if (entity.ProjectId <= Globals.NEW_ID) throw (new ArgumentException("The wiki project id is invalid"));
           

            List<Wiki> wikiHistoryList = new List<Wiki>();
            try
            {
                wikiHistoryList = DataProviderManager.Provider.SaveWikiPageDetails(entity, ref resultValue);

            }
            catch (Exception ex)
            {
                Log.Error(LoggingManager.GetErrorMessageResource("SaveWikiError"), ex);
            }
            return wikiHistoryList;
        }


        public static List<Wiki> GetWikiContent(int projectId, int wikiPageId, ref string wikiPageName)
        {
            
            if (projectId <= Globals.NEW_ID) throw (new ArgumentException("The wiki project id is invalid"));
            if (wikiPageId <= Globals.NEW_ID) throw (new ArgumentException("The wiki project id is invalid"));

            List<Wiki> wikiHistoryList = new List<Wiki>();
            try
            {
                wikiHistoryList = DataProviderManager.Provider.GetWikiContent(projectId, wikiPageId, ref wikiPageName);

            }
            catch (Exception ex)
            {
                Log.Error(LoggingManager.GetErrorMessageResource("SaveWikiError"), ex);
            }
            return wikiHistoryList;
        }

        public static List<Wiki> GetWikiPageDetails(int projectId, string SearchText)
        {
            if (projectId <= Globals.NEW_ID) throw (new ArgumentException("The wiki project id is invalid"));           

            List<Wiki> wikiHistoryList = new List<Wiki>();
            try
            {
                wikiHistoryList = DataProviderManager.Provider.GetWikiPageDetails(projectId, SearchText);

            }
            catch (Exception ex)
            {
                Log.Error(LoggingManager.GetErrorMessageResource("SaveWikiError"), ex);
            }
            return wikiHistoryList;
        }

        public static bool DeleteWikiPage(int wikiPageId)
        {
            if (wikiPageId <= Globals.NEW_ID) throw (new ArgumentException("The wiki page id is invalid"));

            bool isSuccess = false;
            try
            {
                isSuccess = DataProviderManager.Provider.DeleteWikiPage(wikiPageId);

            }
            catch (Exception ex)
            {
                Log.Error(LoggingManager.GetErrorMessageResource("SaveWikiError"), ex);
            }
            return isSuccess;
        }

    }
}
